package com.sphd.emanu.activity.pointcommondity;

import android.content.DialogInterface;
import android.util.Log;

import com.google.gson.JsonObject;
import com.sphd.emanu.mvp.BasePresenterImpl;
import com.sphd.emanu.pojo.goods.Category;
import com.sphd.emanu.pojo.goods.Commodity;
import com.sphd.emanu.utils.GsonUtil;
import com.wzutils.L;

import java.util.List;

import cn.pedant.SweetAlert.SweetAlertDialog;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * MVPPlugin
 *  邮箱 784787081@qq.com
 */

public class PointCommondityPresenter extends BasePresenterImpl<PointCommondityContract.View> implements PointCommondityContract.Presenter{
    @Override
    public void selectPointsCommondity(Integer org) {
        emanuService.selectPointsCommondity(org).enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                L.e(response.toString());
                L.e(response.body());
                List<Commodity> commodityList= GsonUtil.jsonToList(response.body(),Commodity.class);
                mView.setList(commodityList);
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                Error(t);
            }
        });
    }

    @Override
    public void getList(int org) {
            Call<String> call = emanuService.classfylist(org);
            call.enqueue(new Callback<String>() {
                @Override
                public void onResponse(Call<String> call, Response<String> response) {
                    List<Category>  catelist= GsonUtil.jsonToList(response.body().toString(),Category.class);
                    mView.setcatelist(catelist);
                }

                @Override
                public void onFailure(Call<String> call, Throwable t) {
                    Error(t);
                }
            });
    }

    @Override
    public void getCommDityList(int id, final DialogInterface dialog) {
        emanuService.selectCommodity(id).enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                L.e(response.toString());
                List<Commodity> commodityList =GsonUtil.jsonToList(response.body().toString(),Commodity.class);
                mView.commodityList(commodityList,dialog);
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                Error(t);
            }
        });
    }

    @Override
    public void updatePointsCommondity(Integer materialId, int commondityId, Integer points) {
            emanuService.updatePointsCommondity(materialId, commondityId, points).enqueue(new Callback<JsonObject>() {
                @Override
                public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                    mView.upRes(res(response));
                }

                @Override
                public void onFailure(Call<JsonObject> call, Throwable t) {
                    Error(t);
                }
            });
    }

    @Override
    public void addPointsCommondity(int commondityId, int points) {
        emanuService.addPointsCommondity(commondityId, points).enqueue(new Callback<JsonObject>() {
            @Override
            public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                mView.upRes(res(response));
            }

            @Override
            public void onFailure(Call<JsonObject> call, Throwable t) {
                Error(t);
            }
        });
    }

    @Override
    public void delPointsCommondity(int materialId, final Integer postion, final SweetAlertDialog sweetAlertDialog) {
        emanuService.delPointsCommondity(materialId).enqueue(new Callback<JsonObject>() {
            @Override
            public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                mView.delRes(res(response),postion,sweetAlertDialog);
            }

            @Override
            public void onFailure(Call<JsonObject> call, Throwable t) {
                Error(t);
            }
        });
    }
}
